package org.jeecg.modules.activiti.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.activiti.entity.ActNode;
import org.jeecg.modules.activiti.entity.Department;
import org.jeecg.modules.activiti.entity.Role;

import java.util.List;

/**
 * @Description: 流程节点扩展表
 * @Author: jeecg-boot
 * @Date:   2020-03-30
 * @Version: V1.0
 */
public interface ActNodeMapper extends BaseMapper<ActNode> {

    List<LoginUser> findUserByNodeId(@Param("nodeId") String nodeId,@Param("procDefId") String procDefId);

    List<Role> findRoleByNodeId(@Param("nodeId") String nodeId,@Param("procDefId") String procDefId);

    List<Department> findDepartmentByNodeId(@Param("nodeId") String nodeId,@Param("procDefId") String procDefId);

    List<Department> findDepartmentManageByNodeId(@Param("nodeId") String nodeId,@Param("procDefId") String procDefId);

    List<String> findFormVariableByNodeId(@Param("nodeId") String nodeId,@Param("procDefId") String procDefId);

    @Select("select role_code from sys_role where id in (select role_id from sys_user_role where user_id = (select id from sys_user where username=#{username}))")
    List<String> getRoleByUserName(@Param("username") String username);
    @Select("select * from sys_user")
    List<LoginUser> queryAllUser();
    @Select("select * from sys_user where id in (select user_id from sys_user_role where role_id = #{id})")
    List<LoginUser> findUserByRoleId(@Param("id") String id);
    @Select("select * from sys_user where id in (select user_id from sys_user_depart where dep_id = #{id})")
    List<LoginUser> findUserDepartmentId(@Param("id") String id);
    @Select("select * from sys_user where FIND_IN_SET(#{id},depart_ids)")
    List<LoginUser> findUserDepartmentManageId(@Param("id") String id);
    @Select("select * from act_z_node where node_id = #{nodeId}")
    List<ActNode> findByNodeId(@Param("nodeId") String nodeId);
    @Select("select * from sys_user where id in (select dept_manager from sys_depart where id = #{id})")
    List<LoginUser> findUserByDepartId(@Param("id") String id);
    // 把审批意见改为驳回
    @Update("update act_hi_taskinst set delete_reason_ = 'backed' where id_ = #{taskId}")
    void updateTaskBack(@Param("taskId") String taskId);
    @Update("update act_hi_taskinst set delete_reason_ = 'cancel' where id_ = #{taskId}")
    void updateTaskCancel(@Param("taskId") String taskId);

    @Select("select * from act_z_node where node_id = #{nodeId} and proc_def_id = #{procDefId}")
    List<ActNode> findByNodeIdAndProcDefId(@Param("nodeId") String nodeId, @Param("procDefId") String procDefId);

    // update-begin-Author: wmf date: 2021-01-18 16:14 for: 增加节点查询审批人条件

    /**
     * 根据角色查询审批人
     * @param nodeId 节点id
     * @param procDefId 流程定义ID
     * @return
     */
    @Select("select * from sys_user WHERE del_flag = 0 AND org_code = #{orgCode} AND id in " +
            " (SELECT ur.user_id FROM sys_user_role ur, sys_role r  WHERE ur.role_id = r.id AND r.role_code in " +
            " (select relate_id from act_z_node where node_id = #{nodeId} and proc_def_id = #{procDefId} and type = 0))")
    List<LoginUser> findUserByRole(@Param("orgCode") String orgCode,@Param("nodeId") String nodeId,@Param("procDefId") String procDefId);

    /**
     * 根据费用承担部门查询 部门负责人
     * @param deptId 部门Id
     * @return
     */
    @Select(" select * from sys_user WHERE del_flag = 0 AND id in " +
            " (SELECT dept_manager FROM sys_depart t WHERE t.id = #{deptId})")
    List<LoginUser> findUsersByCostDeptManage(@Param("deptId") String deptId);

    /**
     * 根据费用承担部门查询 部门负责人
     * @param deptId 部门Id
     * @return
     */
    @Select(" select * from sys_user WHERE del_flag = 0 AND id in " +
            " (SELECT leaders FROM sys_depart t WHERE t.id = #{deptId})")
    List<LoginUser> findUsersByCostDeptLeaders(@Param("deptId") String deptId);

    // update-end-Author: wmf date: 2021-01-18 16:14 for: 增加节点查询审批人条件

    /**
    * @Description 根据组织id查询对应组织的总经理
    * @author  wujinbiao
    * @date 2022/3/10
    * @param costCompCodeId
    * @return org.jeecg.common.system.vo.LoginUser
    */
    @Select(" select * from sys_user WHERE del_flag = 0 AND id in " +
            " (SELECT general_manager FROM sys_depart t WHERE t.id = #{costCompCodeId})")
    List<LoginUser> findCeoByOrgId(@Param("costCompCodeId") String costCompCodeId);


    /**
     * @Description 根据组织id查询对应组织的副总经理
     * @author  wujinbiao
     * @date 2022/3/10
     * @param costCompCodeId
     * @return org.jeecg.common.system.vo.LoginUser
     */
    @Select(" select * from sys_user WHERE del_flag = 0 AND id in " +
            " (SELECT vice_president FROM sys_depart t WHERE t.id = #{costCompCodeId})")
    List<LoginUser> findFzjlByOrgId(String costCompCodeId);

    /**
     * @Description 根据组织id查询对应组织的总会计师
     * @author  wujinbiao
     * @date 2022/3/10
     * @param costCompCodeId
     * @return org.jeecg.common.system.vo.LoginUser
     */
    @Select(" select * from sys_user WHERE del_flag = 0 AND id in " +
            " (SELECT chief_accountant FROM sys_depart t WHERE t.id = #{costCompCodeId})")
    List<LoginUser> findZkjsByOrgId(String costCompCodeId);


    /**
     * @Description 根据组织id查询对应组织的财务领导
     * @author  wujinbiao
     * @date 2022/3/10
     * @param costCompCodeId
     * @return org.jeecg.common.system.vo.LoginUser
     */
    @Select(" select * from sys_user WHERE del_flag = 0 AND id in " +
            " (SELECT financial_leader FROM sys_depart t WHERE t.id = #{costCompCodeId})")
    List<LoginUser> findCwldByOrgId(String costCompCodeId);


    /**
     * @Description 根据组织id查询对应组织的财务
     * @author  wujinbiao
     * @date 2022/3/10
     * @param costCompCodeId
     * @return org.jeecg.common.system.vo.LoginUser
     */
    @Select(" select * from sys_user WHERE del_flag = 0 AND id in " +
            " (SELECT finance FROM sys_depart t WHERE t.id = #{costCompCodeId})")
    List<LoginUser> findCwByOrgId(String costCompCodeId);


    /**
     * @Description 根据组织id查询对应组织的董事长
     * @author  wujinbiao
     * @date 2022/3/10
     * @param costCompCodeId
     * @return org.jeecg.common.system.vo.LoginUser
     */
    @Select(" select * from sys_user WHERE del_flag = 0 AND id in " +
            " (SELECT chairman FROM sys_depart t WHERE t.id = #{costCompCodeId})")
    List<LoginUser> findDszByOrgId(String costCompCodeId);


    /**
     * @Description 根据组织id查询对应组织的综合办公室
     * @author  wujinbiao
     * @date 2022/3/10
     * @param costCompCodeId
     * @return org.jeecg.common.system.vo.LoginUser
     */
    @Select(" select * from sys_user WHERE del_flag = 0 AND id in " +
            " (SELECT general_office FROM sys_depart t WHERE t.id = #{costCompCodeId})")
    List<LoginUser> findZhbgsByOrgId(String costCompCodeId);
}
